##### Summary statistics (for Appendix) ######

rm(list = ls())

# Load Packages 
library(tidyverse)
library(survey)
library(gtsummary)
library(stargazer)


# Load conjoint data
w7_w8_subgroups <- read_csv("/Users/cbrugge/Desktop/CE Paper/w7_w8_subgroups.csv") %>% 
  as.data.frame() %>% 
  rename(obs = `...1`)

load("/Volumes/gess_ir_sep/data/W8/final_data/sep_w8_research_weighted.RData") 

w8_weights <- dataset_weights %>%   
  select(PubId, w8_weights_raking)

# Save relevant variables
w7_w8_subgroups <- w7_w8_subgroups %>% 
  select(PubId, age, w8_q2, Education, w7_q40x1, political_ideology) 


# Merge df_policy and dataset_weights
w7_w8_weights <- merge(w7_w8_subgroups, w8_weights, by = "PubId")

w7_w8_weights <- w7_w8_weights %>%
  distinct(PubId, .keep_all = TRUE)

# rename variable levels
w7_w8_weights <- w7_w8_weights %>%
  mutate(Education = recode(Education,
                        `1` = "0 - None",
                        `2` = "1 - Obligatory schooling",
                        `3` = "2 - Apprenticeship, vocational school, commercial (middle) school",
                        `4` = "3 - Matura, vocational school-leaving certificate",
                        `5` = "4 - Higher technical/vocational training (e.g. federal certificate, master’s diploma)",
                        `6` = "5 - University of Applied Sciences, University of Education",
                        `7` = "6 - University, ETH",
                        `8` = "7 - Other"
  ))

w7_w8_weights <- w7_w8_weights %>%
  mutate(w7_q40x1 = recode(w7_q40x1,
                           `1` = "0 - Under 2’000 CHF",
                           `2` = "1 - 2001 to 4000 CHF",
                           `3` = "2 - 4001 to 6000 CHF",
                           `4` = "3 - 6001 to 8000 CHF",
                           `5` = "4 - 8001 to 10‘000 CHF",
                           `6` = "5 - 10’001 to 12‘000 CHF",
                           `7` = "6 - 12’001 to 14‘000 CHF",
                           `8` = "7 - 14’001 to 16’000 CHF",
                           `9` = "8 - 16’001 to 18’000 CHF",
                           `10` = "9 - Above 18’000 CHF"
  ))

w7_w8_weights <- w7_w8_weights %>%
  mutate(w8_q2 = recode(w8_q2,
                        `1` = "Female",
                        `2` = "Male",
                        `3` = "Other"
  ))


w7_w8_weights <- w7_w8_weights %>%
  mutate(political_ideology = recode(political_ideology,
                        `center` = "Center",
                        `left` = "Left",
                        `right` = "Right"
  ))


# summary statistics ------------------------------------------------------

survey_design <- svydesign(
  id = ~PubId,             
  data = w7_w8_weights 
)

# create table
tbl <- tbl_svysummary(
  data = survey_design,
  include = c("w8_q2", "age", "Education", "w7_q40x1", "political_ideology"),  
  label = list(
    w8_q2 = "Gender",
    age = "Age",
    Education = "Education",
    w7_q40x1 = "Income",
    political_ideology = "Political Ideology"
  ),
  statistic = list(
    all_categorical() ~ "{n} ({p}%)"
  ),
  digits = NULL,
  missing = "no", 
  missing_text = "(Missing)",
  percent = "column"
)


# Print the table (save in plots using "export as webpage")
tbl
